#include "pch.h"
#include <tchar.h>
#include "CTPService.h"
#include <ThostFtdcUserApiDataType.h>
#include <ThostFtdcTraderApi.h>
#include <ThostFtdcMdApi.h>
#include <ThostFtdcUserApiStruct.h>
#include "TraderSpi.h"

#ifdef _WIN32
#pragma comment(lib, "thostmduserapi_se.lib")
#pragma comment(lib, "thosttraderapi_se.lib")
#endif // _WIN32

bool CTPService::login(const string& user_account, const string& user_password, const ServerInfo& server_info)
{
	CThostFtdcReqUserLoginField login_field;
	memset(&login_field, 0, sizeof(login_field));

	strcpy_s(login_field.BrokerID, server_info.broker_id.c_str());
	strcpy_s(login_field.UserID, user_account.c_str());
	strcpy_s(login_field.Password, user_password.c_str());

	CThostFtdcTraderApi* pTraderApi = CThostFtdcTraderApi::CreateFtdcTraderApi("cc.");
	
	CThostFtdcTraderSpi* pUserSpi = new TraderSpi();
	pTraderApi->RegisterSpi(pUserSpi);
	char front_addr[128];
	strcpy_s(front_addr, server_info.trade_server_front.c_str());
	pTraderApi->RegisterFront(front_addr);
	pTraderApi->Init();

	int ret_code = pTraderApi->ReqUserLogin(&login_field, m_iRequestID++);
	
	::MessageBox(NULL, _T("exec"), _T("CTPService::login"), MB_OK);

	if (ret_code == 0) {
		// 1,2,3
		return true;
	}
	return false;
}

bool CTPService::logout()
{
	return false;
}

bool CTPService::request(int request_id, void * param)
{
	return false;
}

CTPService::CTPService(void)
	:m_iRequestID(0)
{
}

CTPService::~CTPService(void)
{
}
